var path = require('path')
var ImageMin = require('./image-min')

var imageMinOptions = {
  quality: '65-80'
}

module.exports = function (src, dest, options) {
  var srcDir = path.resolve(src)
  var outputDir = dest || path.join(srcDir, '../', 'min-image')
  options.imageMin = options.imageMin || imageMinOptions

  var threshold = options.threshold || 2    // 当前图片与压缩后大小比例如果超过该阈值，则报错提示
  // console.log(src, '\n', dest, '\n', options)

  return ImageMin(srcDir, outputDir, options).then((res) => {
    let needCompress = []
    res.forEach((ele, index) => {
      if ((ele.originalSize / ele.resultSize) > threshold) {
        needCompress.push(index)
      }
    })

    return Promise.resolve({
      images: res,
      threshold: threshold,
      srcDir: srcDir,
      outputDir: outputDir,
      needCompress
    })
  })
}

